<%--
  Created by IntelliJ IDEA.
  User: BHT
  Date: 2022/11/28 0028
  Time: 19:20:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/layui/2.7.6/css/layui.min.css"    media="all">
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
<%--    <div class="layui-btn-container">--%>
<%--        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>--%>
<%--        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>--%>
<%--        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>--%>
<%--    </div>--%>
    <div class="layui-btn-group">
        <button type="button" class="layui-btn" lay-event="add">增加</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<div id="dataUpdate" style="display: none">
    <form class="layui-form" action="${pageContext.request.contextPath}/userUpdateServlet" >
<%--        <input name="id" id="id" type="hidden">--%>
<%--        <div class="layui-form-item">--%>
<%--            <label class="layui-form-label">姓名</label>--%>
<%--            <div class="layui-input-block">--%>
<%--                <input id="username" type="text"  name="title" required  lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input" style="width: 190px ">--%>
<%--            </div>--%>
<%--        </div>--%>
<%--        <div class="layui-form-item">--%>
<%--            <label class="layui-form-label">手机</label>--%>
<%--            <div class="layui-input-inline">--%>
<%--                <input  id="phone" type="text" name="password" required lay-verify="required" placeholder="请输入城市" autocomplete="off" class="layui-input">--%>
<%--            </div>--%>
<%--        </div>--%>
<%--        <div class="layui-form-item">--%>
<%--            <label class="layui-form-label">城市</label>--%>
<%--            <div class="layui-input-inline">--%>
<%--                <input  id="city" type="text" name="city" required lay-verify="required" placeholder="请输入城市" autocomplete="off" class="layui-input">--%>
<%--            </div>--%>
<%--        </div>--%>
<%--        <div class="layui-form-item">--%>
<%--            <label class="layui-form-label">性别</label>--%>
<%--            <div class="layui-input-block">--%>
<%--                <input type="radio" name="sex" value="男" title="男" >--%>
<%--                <input type="radio" name="sex" value="女" title="女">--%>
<%--            </div>--%>
<%--        </div>--%>
<%--        <div class="layui-form-item" id="button">--%>
<%--            <div class="layui-input-block">--%>
<%--                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>--%>
<%--                <button type="reset" class="layui-btn layui-btn-primary">重置</button>--%>
<%--            </div>--%>
<%--        </div>--%>
<%--    </form>--%>
    <input name="id" id="id" type="hidden">
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input id="username" type="text"  name="title" required  lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input" style="width: 190px">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="男" title="男" >
            <input type="radio" name="sex" value="女" title="女">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机</label>
        <div class="layui-input-inline">
            <input  id="phone" type="text" name="phone" required lay-verify="required" placeholder="请输入手机" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">城市</label>
        <div class="layui-input-inline">
            <input  id="city" type="text" name="city" required lay-verify="required" placeholder="请输入城市" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">专业</label>
        <div class="layui-input-inline">
            <input  id="major" type="text" name="major" required lay-verify="required" placeholder="请输入专业" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">职业</label>
        <div class="layui-input-block">
            <select addoccupation id="occupation" name="occupation" lay-verify="required">
                <option value=""></option>
                <option value="数据开发工程师">数据开发工程师</option>
                <option value="算法工程师">算法工程师</option>
                <option value="Web前端开发工程师">Web前端开发工程师</option>
                <option value="Python开发工程师">Python开发工程师</option>
                <option value="数据分析师">数据分析师</option>
                <option value="Java开发工程师">Java开发工程师</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">学历</label>
        <div class="layui-input-block">
            <select name="education" id="education" lay-verify="required">
                <option value=""></option>
                <option value="初中">初中</option>
                <option value="高中">高中</option>
                <option value="专科">专科</option>
                <option value="本科">本科</option>
                <option value="研究生">研究生</option>
                <option value="博士">博士</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">财富</label>
        <div class="layui-input-inline">
            <input  id="money" type="text" name="money" required lay-verify="required" placeholder="请输入财富" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" id="button">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即添加</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
    </form>

</div>
<div id="dataAdd" style="display: none">
    <form class="layui-form" action="${pageContext.request.contextPath}/userAddServlet" >
        <input name="id" id="addid" type="hidden">
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input id="addusername" type="text"  name="title" required  lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input" style="width: 190px">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="男" title="男" >
                <input type="radio" name="sex" value="女" title="女">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机</label>
            <div class="layui-input-inline">
                <input  id="addphone" type="text" name="phone" required lay-verify="required" placeholder="请输入手机" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">城市</label>
            <div class="layui-input-inline">
                <input  id="addcity" type="text" name="city" required lay-verify="required" placeholder="请输入城市" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">专业</label>
            <div class="layui-input-inline">
                <input  id="addmajor" type="text" name="occupation" required lay-verify="required" placeholder="请输入专业" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">职业</label>
            <div class="layui-input-block">
                <select addoccupation name="major" lay-verify="required">
                    <option value=""></option>
                    <option value="数据开发工程师">数据开发工程师</option>
                    <option value="算法工程师">算法工程师</option>
                    <option value="Web前端开发工程师">Web前端开发工程师</option>
                    <option value="Python开发工程师">Python开发工程师</option>
                    <option value="数据分析师">数据分析师</option>
                    <option value="Java开发工程师">Java开发工程师</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">学历</label>
            <div class="layui-input-block">
                <select name="education" lay-verify="required">
                    <option value=""></option>
                    <option value="初中">初中</option>
                    <option value="高中">高中</option>
                    <option value="专科">专科</option>
                    <option value="本科">本科</option>
                    <option value="研究生">研究生</option>
                    <option value="博士">博士</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">财富</label>
            <div class="layui-input-inline">
                <input  id="addmoney" type="text" name="money" required lay-verify="required" placeholder="请输入财富" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" id="addbutton">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即添加</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>


</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/layui/2.7.6/layui.min.js" charset="utf-8"></script>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
    layui.use('table', function(){
        var table = layui.table;
        var form = layui.form;
        table.render({
            elem: '#test'
            ,url:'${pageContext.request.contextPath}/a'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '用户数据表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
                ,{field:'username', title:'用户名', width:120, edit: 'text'}
                ,{field:'sex', title:'性别', width:60, edit: 'text'}
                ,{field:'phone', title:'手机号', width:150, edit: 'text', sort: true}
                ,{field:'city', title:'城市', width:80}
                ,{field:'occupation', title:'职业',width:191}
                ,{field:'major', title:'专业', width:219, sort: true}
                ,{field:'education', title:'学历', width:120}
                ,{field:'money', title:'财富', width:100, sort: true}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:184}
            ]]
            ,page: true
            ,limit:10
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;
                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
            if (obj.event==='add'){
                layer.open({
                    type: 1,
                    area: ['600px', '550px'],
                    content: $('#dataAdd'),
                    cancel:function(){
                        $('#dataAdd').hide();

                    }
                });
            }
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'detail'){//查看
                $('#id').val(data.id)
                $('#username').val(data.username)
                $('#phone').val(data.phone)
                $('#city').val(data.city)
                $('input[name="sex"][value='+data.sex+']').prop('checked','checked')
                $('#major').val(data.major)
                $('#occupation').val(data.occupation)
                $('#education').val(data.education)
                $('#money').val(data.money)
                $('#button').hide()
                $('#id').attr("disabled",true)
                $('#username').attr("disabled",true)
                $('#phone').attr("disabled",true)
                $('#city').attr("disabled",true)
                $('input[name="sex"][value='+'男'+']').attr("disabled",true)
                $('input[name="sex"][value='+'女'+']').attr("disabled",true)
                $('#major').attr("disabled",true)
                $('#occupation').attr("disabled",true)
                $('#education').attr("disabled",true)
                $('#money').attr("disabled",true)
                form.render();
                layer.open({
                    type: 1,
                    area: ['600px', '550px'],
                    content: $('#dataUpdate'),
                    cancel:function(){
                        $('#id').attr("disabled",false)
                        $('#username').attr("disabled",false)
                        $('#phone').attr("disabled",false)
                        $('#city').attr("disabled",false)
                        $('input[name="sex"][value='+'男'+']').attr("disabled",false)
                        $('input[name="sex"][value='+'女'+']').attr("disabled",false)
                        $('#major').attr("disabled",false)
                        $('#occupation').attr("disabled",false)
                        $('#education').attr("disabled",false)
                        $('#money').attr("disabled",false)
                        $('#dataUpdate').hide();
                        $('#button').show();
                    }
                });
            } else if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        type:"GET",
                        dataType:"boolean",
                        url:"${pageContext.request.contextPath}/userDeleteServlet?id="+data.id,
                    })
                    obj.del(index)
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                // layer.alert('编辑行：<br>'+ JSON.stringify(data))
                $('#id').val(data.id)
                $('#username').val(data.username)
                $('#phone').val(data.phone)
                $('#city').val(data.city)
                $('input[name="sex"][value='+data.sex+']').prop('checked','checked')
                $('#major').val(data.major)
                $('#occupation').val(data.occupation)
                $('#education').val(data.education)
                $('#money').val(data.money)
                form.render();
                layer.open({
                    type: 1,
                    area: ['600px', '550px'],
                    content: $('#dataUpdate'),
                    cancel:function(){
                        $('#dataUpdate').hide();

                    }
                });
            }

        });
    });
</script>

</body>
</html>